﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using MathMethods;
using WinAPI32Library;

namespace AnonymousPipesClient
{
    class Program
    {
        // data arrays from file
        public static double[] x, y;


        static void Main(string[] args)
        {
            // get pipe handles from command line
            IntPtr hWritePipe = (IntPtr)Convert.ToInt16(args[0]);
            IntPtr hReadPipe = (IntPtr)Convert.ToInt16(args[1]);
            // type of interpolation
            int type = Convert.ToInt32(args[2]);
            // overlapped
            NativeOverlapped ol = new NativeOverlapped();

            // send ready message
            uint bytesWritten = 0;
            // convert string to bytes
            byte[] bytes = new UTF8Encoding().GetBytes("ready");
            // write to pipe
            bool isWritten = Kernel32.WriteFile(hWritePipe, bytes, (uint)bytes.Length, out bytesWritten, ref ol);


            // read data
            byte[] b = new byte[150];
            uint br = 0;
            string dataRead = "";
            // read pipe
            Kernel32.ReadFile(hReadPipe, b, 150, out br, ref ol);
            // convert to string 
            dataRead = new UTF8Encoding().GetString(b, 0, (int)br).Trim();


            // calculations
            ReadData();
            string answer = "";
            // argument
            double d = Convert.ToDouble(dataRead);
            switch (type)
            {
                case 0:
                    {
                        // lagrange
                        answer += "Lgrng: ";
                        answer += NumericalMethods.LagrangePolynomial(d, x, y);
                        break;
                    }
                case 1:
                    {
                        // newthon
                        answer += "Nwthn: ";
                        answer += NumericalMethods.NewtonPolynomial(d, x, y);
                        break;
                    }
                case 2:
                    {
                        // least squares
                        answer += "Lstsqrs: ";
                        answer += NumericalMethods.LeastSquares(x, y, d);
                        break;
                    }
                case 3:
                    {
                        answer += "Strlng: ";
                        answer += NumericalMethods.StirlingInterpolation(d, x, y);
                        break;
                    }
            }


            // send answer
             bytesWritten = 0;
            // convert string to bytes
            bytes = new UTF8Encoding().GetBytes(answer);
            // write to pipe
            isWritten = Kernel32.WriteFile(hWritePipe, bytes, (uint)bytes.Length, out bytesWritten, ref ol);
            // close handle
            Kernel32.CloseHandle(hWritePipe);
            Kernel32.CloseHandle(hReadPipe);
        }

        public static void ReadData()
        {
            // считываем данные из файла
            string file = @"D:\MSVSProjects\Erofeev\LeastSquares\bin\Debug\data.txt";
            StreamReader r = new StreamReader(file, System.Text.Encoding.UTF8);
            string s = "";
            List<double> xtemp = new List<double>();
            List<double> ytemp = new List<double>();

            while ((s = r.ReadLine()) != null)
            {
                xtemp.Add(Convert.ToDouble(s.Split(' ')[0]));
                ytemp.Add(Convert.ToDouble(s.Split(' ')[1]));
            }
            r.Close();
            x = xtemp.ToArray();
            y = ytemp.ToArray();
        }
    }
}
